package com.prs.crm.action.person;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.person.Dimission;
import com.prs.crm.domain.person.JobTypeTitle;
import com.prs.crm.service.person.DimissionService;

public class DimissionAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	private Integer[]  ids;
	private PaginationSupport pagination;
	private Dimission dimission;
	private DimissionService dimissionService;
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public Dimission getDimission() {
		return dimission;
	}
	public void setDimission(Dimission dimission) {
		this.dimission = dimission;
	}
	public DimissionService getDimissionService() {
		return dimissionService;
	}
	public void setDimissionService(DimissionService dimissionService) {
		this.dimissionService = dimissionService;
	}
	
	@Action("listDimission")
	public String listDimission(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(Dimission.class);
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getDimissionService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	@Action("editDimission")
	public String editDimission(){
		if(ids!=null){
			this.dimission=this.getDimissionService().get(ids[0]);
		}
		return SUCCESS;
	}
	
	@Action(value="saveDimission",results={
			@Result(name="success",type="redirect",location="listDimission"),
			@Result(name="error",type="chain",location="editDimission")
	})
	public String saveDimission(){
		this.getDimissionService().save(dimission);
		dimission.setType(2);
		return SUCCESS;
	}
	
	@Action(value="saveDimDeafult",results={
			@Result(name="success",type="redirect",location="listDimission"),
			@Result(name="error",type="chain",location="listDimission")
	})
	public String saveDimDeafult(){
		if(ids!=null){
			this.dimission=this.getDimissionService().get(ids[0]);
		}
		dimission.setType(1);
		this.getDimissionService().submit(dimission);
		return SUCCESS;
	}
	
	@Action(value="saveDelDimDeafult",results={
			@Result(name="success",type="redirect",location="listDimission"),
			@Result(name="error",type="chain",location="listDimission")
	})
	public String saveDelDimDeafult(){
		List<Dimission> list=this.getDimissionService().getAllDimission();
		for (int i = 0; i < list.size(); i++) {
			Dimission dim=list.get(i);
			if(dim.getRemark()!=null){
				dim.setType(2);
				this.getDimissionService().save(dim);
			}
		}
		return SUCCESS;
	}
	public String removeDimission(){
		if(ids==null){
			this.addActionError("请选择一行进行操作!");
			return ERROR;
		}
		this.getDimissionService().batchRemove(ids);
		return SUCCESS;
	}
}
